//
//  UUProductPackageVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 26/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUProductPackageVC.h"
#import "UUResourceManager.h"
#import "UUPermissionManager.h"
#import "UUProductPackageListVC.h"
#import "UUProductPackageEditVC.h"
#import "UUProductManager.h"

@interface UUProductPackageVC ()

@end

@implementation UUProductPackageVC

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"套餐线路";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([GetPermissionManager() checkPermission:UUPermissionFlagProductReadWrite]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:GetResourceManager().addIconText
                                                                                  target:self
                                                                                selector:@selector(addPackage:)];
    }
    
    NSMutableArray<DKPagingMenuItem *> *tabPageItems = [NSMutableArray arrayWithCapacity:2];
    
    DKPagingMenuItem *verifyItem = [[DKPagingMenuItem alloc] initWithTitle:@"套餐"
                                                        menuViewController:[UUProductPackageListVC packageListWithPackageType:UUProductPackageTypeDay]];
    [tabPageItems addObject:verifyItem];
    
    DKPagingMenuItem *deniedItem = [[DKPagingMenuItem alloc] initWithTitle:@"线路"
                                                        menuViewController:[UUProductPackageListVC packageListWithPackageType:UUProductPackageTypeLine]];
    [tabPageItems addObject:deniedItem];
    
    UUPagingMenuController *tabPageVC = [[UUPagingMenuController alloc] initWithItems:tabPageItems];
    tabPageVC.scrollView.scrollEnabled = NO;
    [self addChildViewController:tabPageVC];
    [self.view addSubview:tabPageVC.view];
    [tabPageVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    [GetProductManager() asyncUpdateProductPackages];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addPackage:(id)sender {
    UIAlertAction *packageAction = [UIAlertAction actionWithTitle:@"套餐"
                                                            style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * _Nonnull action) {
                                                              UUProductPackageEditVC *vc = [UUProductPackageEditVC packageEditWithPackageType:UUProductPackageTypeDay];
                                                              vc.editable = YES;
                                                              [self.navigationController pushViewController:vc animated:YES];
                                                          }];
    
    UIAlertAction *lineAction = [UIAlertAction actionWithTitle:@"线路"
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                           UUProductPackageEditVC *vc = [UUProductPackageEditVC packageEditWithPackageType:UUProductPackageTypeLine];
                                                           vc.editable = YES;
                                                           [self.navigationController pushViewController:vc animated:YES];
                                                       }];
    [UIAlertController presentActionControllerWithTitle:nil actions:@[packageAction, lineAction]];
}

@end
